global class CreateHCPCallCorverAndFrequencyBatchCall implements Database.Batchable<sObject>,Database.Stateful{    
    global void execute(schedulablecontext sc)
    {
          Database.executeBatch(new CreateHCPCallCorverAndFrequencyBatchCall());
     }
     
     global String stringSql{get;set;}
     global CreateHCPCallCorverAndFrequencyBatchCall(){}
  
  global Database.QueryLocator start(Database.BatchableContext BC){
                     
    return Database.getQueryLocator(stringSql);
  }
  global void execute(Database.BatchableContext BC, List<sObject> scope){
      List<HCP_Call_Corver_and_Frequency__c> RecordList = new List<HCP_Call_Corver_and_Frequency__c>();
      String mon;
      String year; 
      
      if(date.today().month() != 1){
          mon = string.valueof(date.today().month() - 1);
          
          year = string.valueof(date.today().year());
      }
      else
      {
          mon = '12';
          year = string.valueof(date.today().year() - 1);
      }
      if(integer.valueof(mon)<10){
        mon = '0'+ mon;
      }
    List<User> userList = [Select id,Name From User Where IsActive =: true];
    Map<id,User> userMap = new Map<id,User>(userList);      
    
    List<R_M_HCP_Number__c> hcpNumList = [Select ownerid,HCP_Total_Number__c,Class_A_Coverage__c,Class_B_Coverage__c,Class_C_Coverage__c,   PlaceID__c,
      Class_D_Coverage__c,Class_DM_Coverage__c,Class_RKOL_Coverage__c,Class_NKOL_Coverage__c,C_Number__c, B_Number__c, A_Number__c,D_Number__c, 
      DM_Number__c,NKOL_Number__c,RKOL_Number__c From R_M_HCP_Number__c Where month__c =: mon and year__c =: year];      
    for(SObject s:scope)
    {
        Call_Report_del__c CallSnap = (Call_Report_del__c)s;
        for(R_M_HCP_Number__c hcpNum : hcpNumList)
        {
            if((CallSnap.ownerid == hcpNum.ownerid)&&(userMap.containsKey(CallSnap.ownerid))&&(hcpNum.PlaceID__c == CallSnap.salescityID__c)){
              /*  A Class  */    
              HCP_Call_Corver_and_Frequency__c newRecordA = new HCP_Call_Corver_and_Frequency__c();
              // create corver
              newRecordA.HCP_Total__c = hcpNum.A_Number__c;
              newRecordA.Cover_HCP__c = hcpNum.Class_A_Coverage__c;   
              //insert frequency
              newRecordA.Class_Total__c = CallSnap.A_Number__c;
              newRecordA.Total_Visted__c = CallSnap.Class_A_Call__c;
              newRecordA.Class__c = 'A';
              newRecordA.manager__c = CallSnap.linemanager__c;
              newRecordA.Place__c = CallSnap.salescityID__c;
              newRecordA.ownerid = CallSnap.ownerid;
              RecordList.add(newRecordA);
              
              /*  B Class  */ 
              HCP_Call_Corver_and_Frequency__c newRecordB = new HCP_Call_Corver_and_Frequency__c();
              // create corver
              newRecordB.HCP_Total__c = hcpNum.B_Number__c;
              newRecordB.Cover_HCP__c = hcpNum.Class_B_Coverage__c;   
              //insert frequency
              newRecordB.Class_Total__c = CallSnap.B_Number__c;
              newRecordB.Total_Visted__c = CallSnap.Class_B_Call__c;
              newRecordB.Class__c = 'B';
              newRecordB.manager__c = CallSnap.linemanager__c;
              newRecordB.Place__c = CallSnap.salescityID__c;
              newRecordB.ownerid = CallSnap.ownerid;
              RecordList.add(newRecordB);
                   
              /*  C Class  */ 
              HCP_Call_Corver_and_Frequency__c newRecordC = new HCP_Call_Corver_and_Frequency__c();
              // create corver
              newRecordC.HCP_Total__c = hcpNum.C_Number__c;
              newRecordC.Cover_HCP__c = hcpNum.Class_C_Coverage__c;   
              //insert frequency
              newRecordC.Class_Total__c = CallSnap.C_Number__c;
              newRecordC.Total_Visted__c = CallSnap.Class_C_Call__c;
              newRecordC.Class__c = 'C';
              newRecordC.manager__c = CallSnap.linemanager__c;
              newRecordC.Place__c = CallSnap.salescityID__c;
              newRecordC.ownerid = CallSnap.ownerid;
              RecordList.add(newRecordC);                    
              /*  D Class  */ 
              HCP_Call_Corver_and_Frequency__c newRecordD = new HCP_Call_Corver_and_Frequency__c();
              // create corver
              newRecordD.HCP_Total__c = hcpNum.D_Number__c;
              newRecordD.Cover_HCP__c = hcpNum.Class_D_Coverage__c;   
              //insert frequency
              newRecordD.Class_Total__c = CallSnap.D_Number__c;
              newRecordD.Total_Visted__c = CallSnap.Class_D_Call__c;
              newRecordD.Class__c = 'D';
              newRecordD.manager__c = CallSnap.linemanager__c;
              newRecordD.Place__c = CallSnap.salescityID__c;
              newRecordD.ownerid = CallSnap.ownerid;
              RecordList.add(newRecordD);   
                                  
              /*  DM Class  */ 
              HCP_Call_Corver_and_Frequency__c newRecordDM = new HCP_Call_Corver_and_Frequency__c();
              // create corver
              newRecordDM.HCP_Total__c = hcpNum.DM_Number__c;
              newRecordDM.Cover_HCP__c = hcpNum.Class_DM_Coverage__c;   
              //insert frequency
              newRecordDM.Class_Total__c = CallSnap.DM_Number__c;
              newRecordDM.Total_Visted__c = CallSnap.Class_DM_Call_NEW__c;
              newRecordDM.Class__c = 'DM';
              newRecordDM.manager__c = CallSnap.linemanager__c;
              newRecordDM.Place__c = CallSnap.salescityID__c;
              newRecordDM.ownerid = CallSnap.ownerid;
              RecordList.add(newRecordDM);   
                                  
              /*  RKOL Class  */ 
              HCP_Call_Corver_and_Frequency__c newRecordRKOL = new HCP_Call_Corver_and_Frequency__c();
              // create corver
              newRecordRKOL.HCP_Total__c = hcpNum.RKOL_Number__c;
              newRecordRKOL.Cover_HCP__c = hcpNum.Class_RKOL_Coverage__c;   
              //insert frequency
              newRecordRKOL.Class_Total__c = CallSnap.RKOL_Number__c;
              newRecordRKOL.Total_Visted__c = CallSnap.Class_RKOL_Call__c;
              newRecordRKOL.Class__c = 'RKOL';
              newRecordRKOL.manager__c = CallSnap.linemanager__c;
              newRecordRKOL.Place__c = CallSnap.salescityID__c;
              newRecordRKOL.ownerid = CallSnap.ownerid;
              RecordList.add(newRecordRKOL);   
                                  
              /*  NKOL Class  */ 
              HCP_Call_Corver_and_Frequency__c newRecordNKOL = new HCP_Call_Corver_and_Frequency__c();
              // create corver
              newRecordNKOL.HCP_Total__c = hcpNum.NKOL_Number__c;
              newRecordNKOL.Cover_HCP__c = hcpNum.Class_NKOL_Coverage__c;   
              //insert frequency
              newRecordNKOL.Class_Total__c = CallSnap.NKOL_Number__c;
              newRecordNKOL.Total_Visted__c = CallSnap.Class_NKOL_Call__c;
              newRecordNKOL.Class__c = 'NKOL';
              newRecordNKOL.manager__c = CallSnap.linemanager__c;
              newRecordNKOL.Place__c = CallSnap.salescityID__c;
              newRecordNKOL.ownerid = CallSnap.ownerid;
              RecordList.add(newRecordNKOL);                                 
          }
        }
        
    }
    insert RecordList;
    RecordList.clear();
    
  }
  global void finish(Database.BatchableContext BC){
     
  }
}